package com.qf.dao.impl;

import com.qf.dao.AddressDao;
import com.qf.entity.Address;
import com.qf.utils.DataSourceUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * @author wgy
 * @version V1.0
 * @project gp12_xiaomi
 * @package com.qf.daoi.impl
 * @description: ${TODO}
 * @date 2022/6/24 14:12
 */
public class AddressDaoImpl implements AddressDao {
    @Override
    public List<Address> selectByUid(Integer uid) {
        QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource());
        try {
            return qr.query("select * from tb_address where uid=?", new BeanListHandler<>(Address.class),uid);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @Override
    public void insert(Address address)  {
        QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource());
        Object[] params={address.getDetail(),address.getName(),address.getPhone(),address.getUid(),address.getLevel()};
        try {
            qr.update("insert into tb_address values(null,?,?,?,?,?);", params);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @Override
    public void delete(int id) {
        QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource());
        try {
            qr.update("delete from tb_address where id=?", id);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }

    }

    @Override
    public void update(Address address) {
        QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource());
        Object[] params={address.getDetail(),address.getName(),address.getPhone(),address.getLevel(),address.getId()};
        try {
            qr.update("update tb_address set detail=?,name=?,phone=?,level=? where id=?", params);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @Override
    public void updateLevel(int id,int uid) {
        QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource());
        //把uid所有的地址设置0
        try {
            qr.update("update tb_address set level=0 where uid=?", uid);
            qr.update("update tb_address set level=1 where id=?", id);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @Override
    public Address selectById(int aid) {
        QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource());
        try {
            return qr.query("select * from tb_address where id=?",new BeanHandler<>(Address.class),aid);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
}
